/**
 * @(#) TrainingMediator.as
 *
 * @date		2009. 04. 03.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	교육 신청 - Mediator
 * 				교육을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.training.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.training.TrainingFacade;
	import com.skt.common_hr.training.events.TrainingEvent;
	import com.skt.common_hr.training.model.ReqTrainingProxy;
	import com.skt.common_hr.training.model.TrainingProxy;
	import com.skt.common_hr.training.model.vo.TrainingListVO;
	import com.skt.common_hr.training.view.components.TrainingOverView;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class TrainingMediator extends OverViewMediator
	{
		public static const NAME:String = "TrainingMediator"; 
		
		private var bExecRequestForm:Boolean = false;		// 현재 교육신청 화면이 실행되어 있는가 여부
		private var bDeletingGarbage:Boolean = false;		// 쓰레기 데이터 삭제중															
		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function TrainingMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		override protected function activatedView():void
		{
			super.activatedView();
 			view.vsTraining.selectedIndex = 0;
			setEventListeners();
			loadReqTrainigList();
		}
		
		override protected function closedView():void
		{
			super.closedView();
			removeEventListeners();
		}
		
 		private function setEventListeners():void
 		{
			view.addEventListener(TrainingOverView.SELECT_REQ_VIEW, onSelectedReqView);
			view.addEventListener(TrainingEvent.DELETE_DATA, onDeleteData);
 		}
 		
		private function removeEventListeners():void
		{
			view.removeEventListener(TrainingOverView.SELECT_REQ_VIEW, onSelectedReqView);
			view.removeEventListener(TrainingEvent.DELETE_DATA, onDeleteData);
		}		
 		


 		//교육신청현황 메인 리스트
 		private function loadReqTrainigList():void{
 			var proxy:ReqTrainingProxy = facade.retrieveProxy(ReqTrainingProxy.NAME) as ReqTrainingProxy;
 			proxy.loadReqTraningList(Variables.myPernr);
 		}
 		



 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat( [
					TrainingFacade.LOAD_REQ_TRAINING_LIST_COMPLETE,
					TrainingFacade.LOAD_REQ_TRAINING_LIST_FAILED,
					TrainingFacade.DELETE_REQ_TRAINING_COMPLETE,
					TrainingFacade.DELETE_REQ_TRAINING_FAILED,
					TrainingFacade.SUCCEED_REQ_TRAINING,
					TrainingFacade.CANCLE_REQ_TRAINING
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			
			switch(note.getName()) 
			{
				case TrainingFacade.LOAD_REQ_TRAINING_LIST_COMPLETE:		// 신청 리스트 조회
					displayReqTrainingList(note.getBody() as ReqTrainingProxy);
					break;
				case TrainingFacade.LOAD_REQ_TRAINING_LIST_FAILED:			// 신청 리스트 조회 실패
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as TrainingProxy).faultMessage);
					break;
				case TrainingFacade.DELETE_REQ_TRAINING_COMPLETE:
					if(!bExecRequestForm) {
						if(bDeletingGarbage) {
							commonFacade.setStatusMessage("STATUS_DELETED_GARBAGE");
						}
						else {
							commonFacade.setStatusMessage("STATUS_DELETED_LISTDATA");
						}
					}
					loadReqTrainigList();
					break;
				case TrainingFacade.DELETE_REQ_TRAINING_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, (note.getBody() as ReqTrainingProxy).faultMessage);
					break;			
				case TrainingFacade.CANCLE_REQ_TRAINING:
					view.vsTraining.selectedIndex = 0;
					loadReqTrainigList();
					break;
				case TrainingFacade.SUCCEED_REQ_TRAINING:
					view.vsTraining.selectedIndex = 0;
					loadReqTrainigList();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
 
		private function displayReqTrainingList(proxy:ReqTrainingProxy):void
		{
			filterGarbage(proxy.reqTrainingList);
			commonFacade.setStatusMessage("STATUS_LOAD_TRAINING_LIST", [proxy.reqTrainingList.length]);  
			view.mainListData = proxy.reqTrainingList; 
		} 
 
 
 		private function filterGarbage(srcArr:ArrayCollection):ArrayCollection
 		{
 			var garbageList:Array = new Array;
 			for (var i:int=0; i<srcArr.length; i++) {
				var vo:TrainingListVO = srcArr.getItemAt(i) as TrainingListVO; 			
 				if(vo.DSTATUS == "1") {
 					srcArr.removeItemAt(i);
 					garbageList.push({GWAREKEY: vo.GWAREKEY});
 				}
 			}
 			
 			if(garbageList.length>0) {
 				deleteGarbage(garbageList);	
 			}
 			return srcArr;
 		}
 		
 		private function deleteGarbage(src:Array):void
 		{
 			trace("쓰레기 데이터를 삭제합니다. " +src.length);
 			bDeletingGarbage = true;
 			var proxy:ReqTrainingProxy = facade.retrieveProxy(ReqTrainingProxy.NAME) as ReqTrainingProxy;
 			proxy.deleteTraining(src); 
 		}
 
 
 
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
 		private function onSelectedReqView(event:Event):void
 		{
 			var reqTrainingMediator:ReqTrainingMediator = facade.retrieveMediator(ReqTrainingMediator.NAME) as ReqTrainingMediator;
 			view.vsTraining.selectedIndex = 1;
 			reqTrainingMediator.execute();
 		}
 		
 		/***
 		 * 데이터 삭제
 		 * **/
 		private function onDeleteData(event:TrainingEvent):void{
 			trace("gkey : " + event.Gkey);
 			CommonAlert.showMsgYesNo(commonFacade.bundle, "ALERT_PROMPT_DELETE", null, "ALERT_TITLE_DEFAULT"
 									, view, function():void{deleteYes(event.Gkey)}, null);
 		}
 		
 		private function deleteYes(gKey:String):void{
 			var proxy:ReqTrainingProxy = facade.retrieveProxy(ReqTrainingProxy.NAME) as ReqTrainingProxy;
 			proxy.deleteTraining([{GWAREKEY: gKey}]);
 		}
		
 		




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():TrainingOverView
        {
            return viewComponent as TrainingOverView;
        }
		
		
	}
}